<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/16
 * Time: 13:40
 */
namespace app\bbs\api;

use app\bbs\model\ForumForum;
use app\common\api\BaseApi;

class ForumForumApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "bbs/ForumForumApi/add";
    /**
     * 保存
     */
    const SAVE = "bbs/ForumForumApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "bbs/ForumForumApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "bbs/ForumForumApi/delete";

    /**
     * 查询
     */
    const QUERY = "bbs/ForumForumApi/query";

    /**
     * 查询不分页
     */
    const QUERY_NO_PAGING = "bbs/ForumForumApi/queryNoPaging";

    /**
     * 查询一条数据
     */
    const GET_INFO = "bbs/ForumForumApi/getInfo";

    /**
     * 分页查询，带上一条主题
     */
    const QUERY_WITH_THREAD = "bbs/ForumForumApi/queryWithThread";

    const QUERY_WITH_COUNT = "bbs/ForumForumApi/queryWithCount";

    /**
     * 添加时候更改版块内容
     */
    const UPDATE = "bbs/ForumForumApi/update";

    const SET_DEC = "bbs/ForumForumApi/setDec";

    /**
     * 统计
     */
    const COUNT = "bbs/ForumForumApi/count";
    const GET_FID_WITH_CHILD = "bbs/ForumForumApi/getFidWithChild";
    const CHECK_BLOCK = "bbs/ForumForumApi/checkBlock";

    protected function _init(){
        $this -> model = new ForumForum();
    }
    // check block status  [1=>lock,-1=>close,0=>open]
    public function checkBlock($fid=0){
        $fid = intval($fid);
        //验证板块信息
        $r  = $this->getInfo(['fid'=>$fid]);
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        if(empty($r['info'])) return $this->apiReturnErr('板块非法');
        $check   = intval($r['info']['modnewposts']);
        $status  = intval($r['info']['status']);
        if(1  === $status) return $this->apiReturnErr('板块锁定中...');
        if(-1 === $status) return $this->apiReturnErr('板块关闭中...');
        $fup = intval($r['info']['fup']);
        if($fup){
            //验证父板块
            $r  = $this->getInfo(['fid'=>$fup]);
            if(!$r['status']) return $this->apiReturnErr($r['info']);
            if(empty($r['info'])) return $this->apiReturnErr('父板块非法');
            // $fcheck = $r['info']['modnewposts'];
            $fstatus = intval($r['info']['status']);
            if(1  === $fstatus) return $this->apiReturnErr('父板块锁定中...');
            if(-1 === $fstatus) return $this->apiReturnErr('父板块关闭中...');
        }
        trace('---check---');
        trace($check);
        return $this->apiReturnSuc($check);
    }
    //get fids with child
    //return  array
    public function getFidWithChild($fid = 0){
        $fid = intval($fid);
        $arr = [$fid];
        if(0 === $fid){
            //全部
            $r = $this->queryNoPaging(null,false,'fid');
        }else{
            $r = $this->queryNoPaging(['fup'=>$fid],false,'fid');
        }
        if(!$r['status']) return $this->apiReturnErr($r['info']);
        foreach ($r['info'] as $v) {
            $arr[] = intval($v['fid']);
        }
        return $this->apiReturnSuc($arr);
    }
    /**
     * query
     * @param 查询条件|null $map
     * @param array|分页参数 $page
     * @param bool|排序参数 $order
     * @param bool|点击分页时带参数 $params
     * @param bool $fields
     * @return array
     * @internal param 查询条件 $map
     * @internal param 分页参数 $page
     * @internal param 排序参数 $order
     * @internal param 点击分页时带参数 $params
     */
    public function queryWithCount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
        $query = $this->model;
        if(!empty($map)){
            if(isset($map['fid'])){
                $p = $map['fid'][1];unset($map['fid']);
                $map['fid'] = ['in',$p];
            }
            $query = $query->where($map);
        }
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();

        if (false === $list) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }
        $count = $this -> model -> where($map) -> count();
        return $this -> apiReturnSuc(["count" => $count, "list" => $list]);
    }
    public function queryWithThread($map = null, $page = array('curpage'=>1,'size'=>6), $order = false, $params = false, $fields = false){

        $query = $this -> model;

        $query = $query -> join(" (select ft.dateline,ft.author,ft.author_id,ft.replies,ft.likes,ft.fid,ft.subject,ft.tid from (select dateline,author,author_id,replies,likes,fid,subject,tid from itboye_forum_thread where status=1 order by dateline desc) as ft group by fid) as thread on itboye_forum_forum.fid=thread.fid","left");

        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if($map !== null){
            $query = $query -> where($map);
        }
        if($order !== false){
            $query = $query -> order($order);
        }
        if($fields !== false){
            $query = $query -> field($fields);
        }
        $list = $query -> select();

        if($list === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        // $Page = new Page($count, $page['size']);

        // //分页跳转的时候保证查询条件
        // if ($params !== false) {
        //     foreach ($params as $key => $val) {
        //         $Page -> parameter[$key] = urlencode($val);
        //     }
        // }

        // // 实例化分页类 传入总记录数和每页显示的记录数
        // $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));

    }


    public function update($fid , $author , $thread = false){

        $map = array(
            'fid' => $fid,
        );
        $entity = array(
            'posts' => array('exp',"posts+1"),
            'lastpost' => $author,
        );
        if($thread !== false){
            $entity['threads'] = array('exp',"threads+1");
        }
        $result = $this -> model -> create($entity, 2);
        if($result === false){
            $error = $this -> model -> getError();
            return $this -> apiReturnErr($error);
        }
        $result = $this -> model -> where($map) -> save();
        if ($result === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        } else {
            return $this -> apiReturnSuc($result);
        }

    }

}